import AppLayout from '@/layouts/index.vue'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from '@/plugins/nprogress'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      component: AppLayout,
      redirect: '/index',
      meta: { layout: 'blank' },

      children: [
        {
          path: '/index',
          name: 'index',
          component: () => import('@/views/front/index.vue'),
          meta: { layout: 'blank' },
        },
        {
          path: '/dashboard',
          name: 'dashboard',
          component: () => import('@/views/Dashboard.vue'),
          meta: { layout: '' },
        },
        {
          path: '/home',
          name: 'home',
          component: () => import('@/views/demo/HomeView.vue'),
          meta: {
            layout: '',
          },
        },
        {
          path: '/icon',
          name: 'icon',
          component: () => import('@/views/demo/icoView.vue'),
          meta: {
            layout: '',
          },
        },
        {
          path: '/theme',
          name: 'theme',
          component: () => import('@/views/demo/themeView.vue'),
          meta: {
            layout: '',
          },
        },
        {
          path: '/uikit/formlayout',
          name: 'formlayout',
          component: () => import('@/views/uikit/FormLayout.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/input',
          name: 'input',
          component: () => import('@/views/uikit/InputDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/button',
          name: 'button',
          component: () => import('@/views/uikit/ButtonDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/table',
          name: 'table',
          component: () => import('@/views/uikit/TableDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/list',
          name: 'list',
          component: () => import('@/views/uikit/ListDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/tree',
          name: 'tree',
          component: () => import('@/views/uikit/TreeDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/panel',
          name: 'panel',
          component: () => import('@/views/uikit/PanelsDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/overlay',
          name: 'overlay',
          component: () => import('@/views/uikit/OverlayDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/media',
          name: 'media',
          component: () => import('@/views/uikit/MediaDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/message',
          name: 'message',
          component: () => import('@/views/uikit/MessagesDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/file',
          name: 'file',
          component: () => import('@/views/uikit/FileDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/menu',
          name: 'menu',
          component: () => import('@/views/uikit/MenuDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/charts',
          name: 'charts',
          component: () => import('@/views/uikit/ChartDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/misc',
          name: 'misc',
          component: () => import('@/views/uikit/MiscDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/uikit/timeline',
          name: 'timeline',
          component: () => import('@/views/uikit/TimelineDoc.vue'),
          meta: { layout: '' },
        },
        {
          path: '/pages/empty',
          name: 'empty',
          component: () => import('@/views/pages/Empty.vue'),
          meta: { layout: '' },
        },
        {
          path: '/pages/crud',
          name: 'crud',
          component: () => import('@/views/pages/Crud.vue'),
          meta: { layout: '' },
        },
        {
          path: '/documentation',
          name: 'documentation',
          component: () => import('@/views/pages/Documentation.vue'),
          meta: { layout: '' },
        },
      ],
    },
    {
      path: '/landing',
      name: 'landing',
      component: () => import('@/views/pages/Landing.vue'),
      meta: { layout: '' },
    },
    {
      path: '/pages/notfound',
      name: 'notfound',
      component: () => import('@/views/pages/NotFound.vue'),
      meta: { layout: '' },
    },
    {
      path: '/auth/login',
      name: 'login',
      component: () => import('@/views/pages/auth/Login.vue'),
      meta: { layout: '' },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/pages/auth/Login.vue'),
      meta: { layout: '' },
    },
    {
      path: '/auth/access',
      name: 'accessDenied',
      component: () => import('@/views/pages/auth/Access.vue'),
      meta: { layout: '' },
    },
    {
      path: '/auth/error',
      name: 'error',
      component: () => import('@/views/pages/auth/Error.vue'),
      meta: { layout: '' },
    },
  ],
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  next()
})

router.afterEach(() => {
  NProgress.done()
})

/**
 * @description 路由跳转错误
 */
router.onError(error => {
  NProgress.done()
  console.warn('路由错误', error.message)
})

export default router
